package com.zmyuan.quartz.ram.demo.cron;

import com.zmyuan.quartz.ram.demo.HelloJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

import static org.quartz.JobBuilder.newJob;

/**
 * Created by zhudebin on 2017/2/25.
 */
public class CronMonthDemo {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.load(CronMonthDemo.class.getResourceAsStream("/quartz.properties"));

        StdSchedulerFactory schedFact = new StdSchedulerFactory(properties);


        Scheduler sched = schedFact.getScheduler();
        sched.start();

        // define the job and tie it to our HelloJob class
        JobDetail job = newJob(HelloJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData("squidFlowId", 111)
                .build();

        String cronExp = "30 40 17 2 * ? *";
        // 触发时间点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder
                .cronSchedule(cronExp)
                .inTimeZone(TimeZone.getDefault())
//                .withMisfireHandlingInstructionDoNothing()
                ;
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(cronScheduleBuilder)
                .build();

        // Tell quartz to schedule the job using our trigger
        Date fire = sched.scheduleJob(job, trigger);
        if(fire != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.format(fire));
        }



        System.out.println("schedule job");
    }
}
